package com.cloud.service;

import com.aliyun.auth.credentials.Credential;
import com.aliyun.auth.credentials.provider.StaticCredentialProvider;
import com.aliyun.sdk.service.dysmsapi20170525.AsyncClient;
import com.aliyun.sdk.service.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.sdk.service.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.sdk.service.dysmsapi20170525.models.SendSmsResponseBody;
import com.cloud.result.BaseResult;
import darabonba.core.client.ClientOverrideConfiguration;
import lombok.SneakyThrows;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.concurrent.CompletableFuture;

@DubboService
@Service
public class MessageServiceImpl implements MessageService {
  @Value("${message.accessKeyId}")
  private String accessKeyId;
  @Value("${message.accessKeySecret}")
  private String accessKeySecret;




  @SneakyThrows
  @Override
  public BaseResult sendMessage(String phoneNumber, String code) {
    // Configure Credentials authentication information, including ak, secret, token
    StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder()
        // Please ensure that the environment variables ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET are set.
         .accessKeyId(accessKeyId)
         .accessKeySecret(accessKeySecret)
        //.securityToken(System.getenv("ALIBABA_CLOUD_SECURITY_TOKEN")) // use STS token
         .build());


    // Configure the Client
    AsyncClient client = AsyncClient.builder()
         .region("cn-hangzhou") // Region ID
        //.httpClient(httpClient) // Use the configured HttpClient, otherwise use the default HttpClient (Apache HttpClient)
         .credentialsProvider(provider)
        //.serviceConfiguration(Configuration.create()) // Service-level configuration
        // Client-level configuration rewrite, can set Endpoint, Http request parameters, etc.
         .overrideConfiguration(
            ClientOverrideConfiguration.create()
                // Endpoint 请参考 https://api.aliyun.com/product/Dysmsapi
                 .setEndpointOverride("dysmsapi.aliyuncs.com")
            //.setConnectTimeout(Duration.ofSeconds(30))
         )
         .build();


    // Parameter settings for API request
    SendSmsRequest sendSmsRequest = SendSmsRequest.builder()
         .signName("阿里云短信测试")
         .templateCode("SMS_154950909")
         .phoneNumbers(phoneNumber)
         .templateParam("{\"code\":\"" + code + "\"}")
        // Request-level configuration rewrite, can set Http request parameters, etc.
        // .requestConfiguration(RequestConfiguration.create().setHttpHeaders(new HttpHeaders()))
         .build();


    // Asynchronously get the return value of the API request
    CompletableFuture<SendSmsResponse> response = client.sendSms(sendSmsRequest);
    // Synchronously get the return value of the API request
    SendSmsResponse resp = response.get();
    // Finally, close the client
    client.close();


    SendSmsResponseBody respBody = resp.getBody();
    if ("OK".equals(respBody.getCode())) {
      return new BaseResult(200, respBody.getCode(), respBody.getMessage());
     } else {
      return new BaseResult(500, respBody.getCode(), respBody.getMessage());
     }
   }
}
